

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/figure_a1.log", text replace

/*------------------------------------------------------------------------------
	2. Clean Main Datasets
------------------------------------------------------------------------------*/

	*Loop through years:
	forvalues year = 2017/2021 {
		
		*Import Data
		use "$data/childrens`year'.dta", clear

			/*Restrict Sample*/
				*Keep f1095 filings
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if `year'*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12

			/*Generate variables for figure*/	
				*Generate outcome variable on claiming
					gen onreturn = file_inc != .
					gen months_unins = 12 - num_cov_ins

				*Generate a count variable
					gen ct = 1
	
				*Collapse to the by-age_num level		
					collapse (sum) ct onreturn, by(months_unins)

				*Adjust for claiming limits
					replace onreturn = onreturn * 1.0017029

				*Generate outcome (i.e., percent of children claimed)
					gen perc_return = 100 * onreturn/ct
	
				reg perc_return months_unins [pw = ct]
					local b = _b[months_unins]
					local cons = _b[_cons]

/*------------------------------------------------------------------------------
	3. Create Figure A.1
------------------------------------------------------------------------------*/

			/*Create figure*/	
				twoway (scatter perc_return months_unins, msymbol(o) mlcolor(gs1) mfcolor(white) clcolor(gs1) lpattern(dash)) ///
					(function y = `b'*x + `cons', range(0 12) lpattern(dash) lcolor(gs10) lwidth(vthin)) ///
					(lfit perc_return months_unins [pw = ct], lcolor(gs1) lpattern(dash)), ///
					 ylabel(, format(%5.1f) labsize(small)) ///
					xtitle("Months Uninsured", height(4)) ///
					xlabel(0(1)12, labsize(small)) ///
					ytitle("Share Claimed (%)", size(medsmall)) ///
					legend(off) ///
					graphregion(fcolor(white) color(white) lcolor(white))


				*Export Graph
					graph export "${output}/`year' By Months Uninsured.png", replace as(png)

